/*
 * @author : TF-BJ-C064
 * @creation : 2014-7-16 下午1:48:57
 * @description : 
 *
 */

package com.tool;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTimeTools {
	
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd EE HH:mm:ss");
	public static void main(String[] args) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EE ");
		
		Timestamp inTime = new Timestamp(System.currentTimeMillis());
		Timestamp outStartTime = new Timestamp(System.currentTimeMillis());
		Timestamp outEndTime = new Timestamp(System.currentTimeMillis());
		
		System.out.println("----------this week----");
		getWeekDays(inTime, outStartTime, outEndTime);
		System.out.println("outStartTime = " + dateFormat.format(outStartTime) );
		System.out.println("outEndTime   = " + dateFormat.format(outEndTime) );
		
		System.out.println("----------last week----");
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DATE, c.get(Calendar.DATE)-7);
		getWeekDays( new Timestamp(c.getTimeInMillis()), outStartTime, outEndTime);
		System.out.println("outStartTime = " + dateFormat.format(outStartTime) );
		System.out.println("outEndTime   = " + dateFormat.format(outEndTime) );
		
		System.out.println("----------this month----");
		getMonthDays(inTime, outStartTime, outEndTime);
		System.out.println("outStartTime = " + dateFormat.format(outStartTime) );
		System.out.println("outEndTime   = " + dateFormat.format(outEndTime) );
		System.out.println("----------last month----");
		c = Calendar.getInstance();
		c.set(Calendar.DATE, -1);
		getMonthDays( new Timestamp(c.getTimeInMillis()), outStartTime, outEndTime);
		System.out.println("outStartTime = " + dateFormat.format(outStartTime) );
		System.out.println("outEndTime   = " + dateFormat.format(outEndTime) );
		
		System.out.println("----------this year----");
		getYearMonths(inTime, outStartTime, outEndTime);
		System.out.println("outStartTime = " + dateFormat.format(outStartTime) );
		System.out.println("outEndTime   = " + dateFormat.format(outEndTime) );
		
		
	}
	/**
	 * 获取一周的开始和结束日期
	 * <p>Title: getWeekDays</p>
	 * <p>Description: </p>
	 * @param inTime 传入需要获取周中的任意一天的时间即可,如本周直接传入 new Timestamp(System.currentTimeMillis())
	 * @param outStartTime
	 * @param outEndTime
	 * @return
	 */
	public static boolean getWeekDays(Timestamp inTime, Timestamp outStartTime, Timestamp outEndTime){
		if(inTime==null){
			return false;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(inTime.getTime());
		
		//setToFirstDay
		while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			calendar.add(Calendar.DATE, -1);
		}
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		if(outStartTime!=null)
			outStartTime.setTime(calendar.getTimeInMillis());//set start time
		
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		calendar.add(Calendar.DATE, 6);
		if(outEndTime!=null)
			outEndTime.setTime(calendar.getTimeInMillis());//set end time
		return true;
	}
	/**
	 * 获取一个月的开始和结束日期
	 * <p>Title: getMonthDays</p>
	 * <p>Description: </p>
	 * @param inTime   传入需要获取中的任意一天的时间即可
	 * @param outStartTime
	 * @param outEndTime	 * @return
	 */
	public static boolean getMonthDays(Timestamp inTime, Timestamp outStartTime, Timestamp outEndTime){
		if(inTime==null){
			return false;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(inTime.getTime());
		calendar.set(Calendar.DATE, 1);
		
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		if(outStartTime!=null)
			outStartTime.setTime(calendar.getTimeInMillis());//set start time
		int lastday = calendar.getActualMaximum(Calendar.DATE);
		while( calendar.get(Calendar.DATE)!=lastday){
			calendar.add(Calendar.DATE, 1);
		}
		
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		if(outEndTime!=null)
			outEndTime.setTime(calendar.getTimeInMillis());///set end time
		return true;
	}
	
	/**
	 * 获取一个年的开始和结束
	 * <p>Title: getYearMonths</p>
	 * <p>Description: </p>
	 * @param inTime
	 * @param outStartTime
	 * @param outEndTime
	 * @return
	 */
	public static boolean getYearMonths(Timestamp inTime, Timestamp outStartTime, Timestamp outEndTime){
		if(inTime==null ){
			return false;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(inTime.getTime());
		calendar.set(Calendar.MONTH, 0);
		
		calendar.set(Calendar.DATE, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		outStartTime.setTime(calendar.getTimeInMillis());//set start time
		
		calendar.set(Calendar.MONTH, 11);
		
		getMonthDays( new Timestamp(calendar.getTimeInMillis()), null, outEndTime );//set end time
		
		return true;
	}
	
	
}
